package autokeyboard.data;

import java.awt.Event;
import java.util.ArrayList;

import com.melloware.jintellitype.HotKeyListener;
import com.melloware.jintellitype.JIntellitype;

/**
 * This Class is used to type all character in your table
 * <p>
 * It also register your own hot key when to quick start or stop
 * 
 * @author HoldTheHand
 * @version v1.0.0
 */
public class RobotType implements HotKeyListener
{
	private int			startKey	= Event.F1;
	private int			pauseKey	= Event.F2;
	private int			stopKey		= Event.F3;

	private RobotData	data		= new RobotData();
	private RobotTyping	robot;
	
	private ArrayList<String[]> listKeyAndDelay;

	public RobotType()
	{
		addHotKeyListener();
	}
	
	public void newListKey(ArrayList<String[]> listKeyAndDelay)
	{
		this.listKeyAndDelay = listKeyAndDelay;
		robot = new RobotTyping(this.listKeyAndDelay);
	}

	@Override
	public void onHotKey(int identifier)
	{
		if (identifier == startKey)
		{
			start();
		}
		else if (identifier == pauseKey)
		{
			pause();
		}
		else if (identifier == stopKey)
		{
			stop();
		}
	}

	private int registerHotKey(String keyName)
	{
		int keyCode = data.keyRegister(keyName);
		JIntellitype.getInstance().registerHotKey(keyCode, keyName);
		return keyCode;
	}

	/**
	 * This method will register your start hotKey with your input keyName
	 * 
	 * @param keyName
	 */
	public void setStartKey(String keyName)
	{
		startKey = registerHotKey(keyName);
	}

	/**
	 * This method will register your pauseKey with your input keyName
	 * 
	 * @param keyName
	 */
	public void setPauseKey(String keyName)
	{
		pauseKey = registerHotKey(keyName);
	}

	/**
	 * This method will register your stopKey with your input keyName
	 * 
	 * @param keyName
	 */
	public void setStopKey(String keyName)
	{
		stopKey = registerHotKey(keyName);
	}

	/**
	 * This method will start your auto typing when you press your start hot key
	 */
	public void start()
	{
		robot.start();
		Thread thread = new Thread(robot);
		thread.start();
	}

	/**
	 * This method will pause your current auto typing when you press your pause
	 * hot key
	 */
	public void pause()
	{
		robot.pause();
	}

	/**
	 * This method will stop your current auto typing when you press your stop
	 * hot key
	 */
	public void stop()
	{
		robot.stop();
	}

	/**
	 * This method will add HotKeyListener for this Class
	 */
	private void addHotKeyListener()
	{
		try
		{
			JIntellitype.getInstance().addHotKeyListener(this);
		} catch (Exception ex)
		{
		}
	}
}
